package com.eds.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eds.mapper.TeacherScheduleMapper;
import com.eds.service.TeacherScheduleService;
import com.pojo.TeacherSchedule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 34195
 * @description 针对表【teacher_schedule(教师课程表)】的数据库操作Service实现
 * @createDate 2025-03-25 19:34:29
 */
@Service
public class TeacherScheduleServiceImpl extends ServiceImpl<TeacherScheduleMapper, TeacherSchedule>
        implements TeacherScheduleService {


    @Autowired
    private TeacherScheduleMapper teacherScheduleMapper;


    /* *
     * @Title: insertSchedule
     * @Author: vvSmile
     * @Date: 2025-03-26 21:00:23
     * @param schedule
     * @return Boolean
     * @Description: TODO 处理课程安排数据
     */
    @Override
    public Boolean insertSchedule(TeacherSchedule schedule) {
        int insert = teacherScheduleMapper.insert(schedule);
        return insert > 0;
    }


    /* *
     * @Title: selectTeacherSchedules
     * @Author: vvSmile
     * @Date: 2025-03-27 10:26:37
     * @param courseId
     * @return List<TeacherSchedule>
     * @Description: TODO 查询课程时间
     */
    @Override
    public List<TeacherSchedule> selectTeacherSchedules(Long courseId) {
        QueryWrapper<TeacherSchedule> scheduleQueryWrapper = new QueryWrapper<>();
        scheduleQueryWrapper.eq("course_id", courseId);
        List<TeacherSchedule> teacherSchedules = teacherScheduleMapper.selectList(scheduleQueryWrapper);
        return teacherSchedules;
    }


    /* *
     * @Title: updateScheduleStatusDel
     * @Author: vvSmile
     * @Date: 2025-03-29 21:06:24
     * @param courseId
     * @return Boolean
     * @Description: TODO 更新课程时间状态为-1
     */
    @Override
    public Boolean updateScheduleStatusDel(Long courseId) {
        // 创建 UpdateWrapper 对象并设置条件
        UpdateWrapper<TeacherSchedule> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("course_id", courseId);
        TeacherSchedule teacherSchedule = new TeacherSchedule();
        teacherSchedule.setStatus(-1);
        int i = teacherScheduleMapper.update(teacherSchedule, updateWrapper);
        return i > 0;
    }


    /* *
     * @Title: updateScheduleInfo
     * @Author: vvSmile
     * @Date: 2025-03-30 15:31:23
     * @param teacherSchedule
     * @return Boolean
     * @Description: TODO 更新课程时间
     */
    @Override
    public Boolean updateScheduleInfo(TeacherSchedule teacherSchedule) {
        QueryWrapper<TeacherSchedule> scheduleQueryWrapper = new QueryWrapper<>();
        scheduleQueryWrapper.eq("teacher_id", teacherSchedule.getTeacherId())
                .eq("course_id", teacherSchedule.getCourseId())
                .eq("schedule_id", teacherSchedule.getScheduleId());
        TeacherSchedule teacherSchedule1 = teacherScheduleMapper.selectOne(scheduleQueryWrapper);
        if (teacherSchedule1 == null) {
            int insert = teacherScheduleMapper.insert(teacherSchedule);
        }
        UpdateWrapper<TeacherSchedule> teacherScheduleUpdateWrapper = new UpdateWrapper<>();
        teacherScheduleUpdateWrapper.eq("teacher_id", teacherSchedule.getTeacherId())
                .eq("course_id", teacherSchedule.getCourseId())
                .eq("schedule_id", teacherSchedule.getScheduleId());
        int update = teacherScheduleMapper.update(teacherSchedule, teacherScheduleUpdateWrapper);
        return update > 0;
    }


    /* *
     * @Title: deleteAllSchedule
     * @Author: vvSmile
     * @Date: 2025-03-30 19:06:49
     * @param courseId
     * @return int
     * @Description: TODO 删除所有的Schedule
     */
    @Override
    public int deleteAllSchedule(Long courseId) {
        // 使用MyBatis Plus的条件构造器删除所有旧记录
        LambdaQueryWrapper<TeacherSchedule> scheduleWrapper = new LambdaQueryWrapper<>();
        scheduleWrapper.eq(TeacherSchedule::getCourseId, courseId);
        int delete = teacherScheduleMapper.delete(scheduleWrapper);
        return delete;
    }


    /* *
     * @Title: selectTeacherSchedulesByScheduleId
     * @Author: vvSmile
     * @Date: 2025-03-31 18:56:09
     * @param scheduleId
     * @return TeacherSchedule
     * @Description: TODO 根据scheduleId查询课程安排
     */
    @Override
    public TeacherSchedule selectTeacherSchedulesByScheduleId(Long scheduleId) {
        QueryWrapper<TeacherSchedule> scheduleQueryWrapper = new QueryWrapper();
        scheduleQueryWrapper.eq("schedule_id", scheduleId);
        TeacherSchedule teacherSchedule = teacherScheduleMapper.selectOne(scheduleQueryWrapper);
        return teacherSchedule;
    }


    /* *
     * @Title: updateScheduleStatusReverseBooking
     * @Author: vvSmile
     * @Date: 2025-03-31 19:28:40
     * @param teacherSchedule
     * @return Boolean
     * @Description: TODO 更新课程安排状态为0
     */
    @Override
    public Boolean updateScheduleStatusReverseBooking(TeacherSchedule teacherSchedule) {
        UpdateWrapper<TeacherSchedule> teacherScheduleUpdateWrapper = new UpdateWrapper<>();
        teacherScheduleUpdateWrapper.eq("schedule_id", teacherSchedule.getScheduleId())
                .eq("start_time", teacherSchedule.getStartTime())
                .eq("end_time", teacherSchedule.getEndTime());
        int update = teacherScheduleMapper.update(teacherSchedule, teacherScheduleUpdateWrapper);
        return update > 0;
    }


    // 根据scheduleId查询课程id
    @Override
    public TeacherSchedule selectCourseIdByScheduleId(Long scheduleId) {
        QueryWrapper<TeacherSchedule> scheduleQueryWrapper = new QueryWrapper<>();
        scheduleQueryWrapper.eq("schedule_id", scheduleId);
        TeacherSchedule teacherSchedule = teacherScheduleMapper.selectOne(scheduleQueryWrapper);

            return teacherSchedule;

    }


    /* *
     * @Title: delSchedule
     * @Author: vvSmile
     * @Date: 2025-04-02 10:13:35
     * @param courseId
     * @return Boolean
     * @Description: TODO 根据courseId删除课程安排
     */
    @Override
    public Boolean delSchedule(TeacherSchedule teacherSchedule) {
        int i = teacherScheduleMapper.deleteById(teacherSchedule);
        return i > 0;
    }

    @Override
    public TeacherSchedule selectTeacherSchedulesBySchedule(TeacherSchedule teacherSchedule) {
        QueryWrapper<TeacherSchedule> scheduleQueryWrapper = new QueryWrapper<>();
        scheduleQueryWrapper.eq("teacher_id", teacherSchedule.getTeacherId())
                .eq("course_id", teacherSchedule.getCourseId())
                .eq("date", teacherSchedule.getDate())
                .eq("start_time", teacherSchedule.getStartTime())
                .eq("end_time", teacherSchedule.getEndTime())
                .eq("repeat_type", teacherSchedule.getRepeatType());
        TeacherSchedule teacherSchedule1 = teacherScheduleMapper.selectOne(scheduleQueryWrapper);
        return teacherSchedule1;
    }

    @Override
    public TeacherSchedule selectTeacherSchedulesByScheduleAndUserId(TeacherSchedule teacherSchedule) {
        QueryWrapper<TeacherSchedule> scheduleQueryWrapper = new QueryWrapper<>();
        scheduleQueryWrapper.eq("schedule_id", teacherSchedule.getScheduleId());
        TeacherSchedule teacherSchedule1 = teacherScheduleMapper.selectOne(scheduleQueryWrapper);
        if (teacherSchedule1.getRepeatType().equals("once")) {
            return teacherSchedule1;

        } else {
            TeacherSchedule teacherSchedule2 = new TeacherSchedule();
            teacherSchedule2.setRepeatType("once");
            teacherSchedule2.setDate(teacherSchedule.getDate());
            teacherSchedule2.setStartTime(teacherSchedule1.getStartTime());
            teacherSchedule2.setEndTime(teacherSchedule1.getEndTime());
            teacherSchedule2.setTeacherId(teacherSchedule1.getTeacherId());
            teacherSchedule2.setCourseId(teacherSchedule1.getCourseId());
            QueryWrapper<TeacherSchedule> scheduleQueryWrapperOnce = new QueryWrapper<>();
            scheduleQueryWrapperOnce.eq("teacher_id", teacherSchedule2.getTeacherId())
                    .eq("course_id", teacherSchedule2.getCourseId())
                    .eq("start_time", teacherSchedule2.getStartTime())
                    .eq("end_time", teacherSchedule2.getEndTime())
                    .eq("date", teacherSchedule2.getDate())
                    .eq("repeat_type", "once")
                    .eq("user_id", teacherSchedule.getUserId());
            TeacherSchedule teacherSchedule3 = teacherScheduleMapper.selectOne(scheduleQueryWrapperOnce);
            return teacherSchedule3;
        }
    }


    /* *
     * @Title: selectTeacherSchedulesByUserId
     * @Author: vvSmile
     * @Date: 2025-04-04 12:55:13
     * @param userId
     * @return Boolean
     * @Description: TODO 根据userId查询课程安排
     */
    @Override
    public List<TeacherSchedule> selectTeacherSchedulesByUserId(Long userId) {

        QueryWrapper<TeacherSchedule> scheduleQueryWrapper = new QueryWrapper<>();
        scheduleQueryWrapper.eq("user_id", userId)
                .eq("status", 1);
        List<TeacherSchedule> teacherSchedules = teacherScheduleMapper.selectList(scheduleQueryWrapper);
        if (teacherSchedules != null) {
            return teacherSchedules;
        }
        return null;
    }


    /* *
     * @Title: updateScheduleStatus
     * @Author: vvSmile
     * @Date: 2025-04-07 10:40:43
     * @param scheduleId
     * @param status
     * @return int
     * @Description: TODO 更新课程安排状态
     */
    @Override
    public int updateScheduleStatus(Long scheduleId, int status) {
        UpdateWrapper<TeacherSchedule> teacherScheduleUpdateWrapper = new UpdateWrapper<>();
        teacherScheduleUpdateWrapper.eq("schedule_id", scheduleId)
                .set("status", status);
        int update = teacherScheduleMapper.update(null, teacherScheduleUpdateWrapper);
        return update;
    }
}




